<template>
  <div style="padding: 10px">
    <el-row :gutter="10">
      <el-col :span="12">
        <el-card>
          <div slot="header">渠道列表</div>
          <div>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
              <el-form-item label="渠道名称" prop="channelName">
                <el-checkbox-group v-model="form.channelIds">
                  <el-checkbox
                    v-for="item in channelList"
                    :key="item.id"
                    :label="item.id"
                    >{{ item.channelName }}</el-checkbox
                  >
                </el-checkbox-group>
              </el-form-item>
              <el-form-item style="text-align: right">
                <el-button type="primary" @click="handleSubmit">保存</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { listChannel, saveChannel } from "@/api/mallSystem/mallChannel";
import { allChannelList } from "@/api/mallGuan/channel";
export default {
  data() {
    return {
      form: {
        channelIds: [],
      },
      channelList: [],
      rules: {
        channelIds: [
          { required: true, message: "请选择渠道", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getSelectedChannel();
  },
  methods: {
    getList() {
      allChannelList().then((res) => {
        this.channelList = res.data;
      });
    },
    //获取已选渠道
    getSelectedChannel() {
      listChannel().then((res) => {
        if (res.data.length > 0) {
          this.form.channelIds = res.data;
        } else {
          this.form.channelIds = [];
        }
      });
    },
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          saveChannel(this.form.channelIds).then((res) => {
            this.$message.success("保存成功");
          });
        }
      });
    },
  },
};
</script>
